/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework.representation;

import java.util.List;

import main.framework.http.ContentType;
import main.framework.http.MessageBody;
import main.framework.resource.Resource;
import main.framework.resource.ResourceData;

/**
 * This class represents a text representation creator which is the standard representation creator of the web service.
 */
public class TextRepresentationCreator extends RepresentationCreator {

	/**
	 * Creates a default representation creator for the given content type.
	 * 
	 * @param contentType The desired content type for the representations to be created by the representation creator.
	 */
	public TextRepresentationCreator(ContentType contentType) {
		super(contentType);
	}

	@Override
	public MessageBody createRepresentation(String data) {
		return new MessageBody(data, super.contentType);
	}

	@Override
	public MessageBody createRepresentation(ResourceData resourceData) {
		String uriNode = resourceData.getResource().getURINode().getUriNode();
        List<Resource> childrenResources = resourceData.getResource().getChildren();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(uriNode);
		stringBuilder.append("\n");
		
		for (int i = 0; i < resourceData.getData().size(); i++) {
			for (int j = 0; j < resourceData.getData().get(i).length; j++){
				if(childrenResources.size() == 0){
					if (i > 0){
						stringBuilder.append("\t");
						stringBuilder.append(resourceData.getData().get(0)[j]);
						stringBuilder.append(" = ");
						stringBuilder.append(resourceData.getData().get(i)[j]);
						stringBuilder.append("\n");
					}
				} else {
					stringBuilder.append("\t");
					stringBuilder.append(childrenResources.get(i).getURINode().getUriNode());
					stringBuilder.append("\n");
				}
			}
		}
		return new MessageBody(stringBuilder.toString(), super.contentType);
	}

}